#-*- coding:UTF-8 -*-

import os, shutil
import os.path
import os, zipfile
import os, sys


#遍历指定目录，显示目录下的所有文件名
def eachFile(filepath):
	for parent,dirnames,filenames in os.walk(filepath):
		#case 1:
		# for dirname in dirnames:
			# print("路径:" + parent)
			# print("文件夹:" + dirname)
		#case 2
		for filename in filenames:
			print("父级:" + parent)
			print("路径:"+ os.path.join(parent,filename)+'\n')

# #遍历指定目录，显示目录下的所有文件名
# def eachFile1(filepath):
#     for parent, dirnames, filenames in os.walk(filepath,  followlinks=True):
#         for filename in filenames:
#             file_path = os.path.join(parent, filename)
#             print('文件名：%s' % filename)
#             print('文件完整路径：%s\n' % file_path)

#打包目录为zip的文件（未压缩）
def make_zip(source_dir, output_filename):
	zipf = zipfile.ZipFile(output_filename, 'w')
	pre_len = len(os.path.dirname(source_dir))
	for parent, dirnames, filenames in os.walk(source_dir):
		for filename in filenames:
			pathfile = os.path.join(parent, filename)
			arcname = pathfile[pre_len:].strip(os.path.sep) #相对路径
			zipf.write(pathfile, arcname)
	zipf.close()

def remove(source_dir):
	for parent,dirnames,filenames in os.walk(source_dir):
		for filename in filenames:
			filePath = os.path.join(parent,filename)
			os.remove(filePath)
			shutil.rmtree(parent,True)
			shutil.rmtree(source_dir,True)


if __name__ == '__main__':
    filePath = "/var/www/py3env/python/dir/file.text"
    writePath = "/var/www/py3env/python/dir/write.text"
    eachPath = "/var/www/py3env/python/dir/"
    # eachFile(eachPath)
    # eachFile1(eachPath)
    # make_zip(eachPath, '测试压缩文件.zip')
    remove(eachPath)
